import React from 'react';
import { BaseTable, utils, DifferenceSingle } from '@/components/BaseTable';
import _ from 'lodash';

const DifferenceOlive = props => {
  const { allgoodsValue, tabkey } = props;
  const titleTree = [
    {
      name: '版本',
      key: 'edition',
      colspan: 1,
      rowspan: 3,
    },
    {
      name: '全盘货值 (含可售车位)',
      key: 'entireProject',
      colspan: 1 * 9,
    },
    {
      name: '未推售车位货值',
      key: 'ValueOfUnsoldParkingSpace',
      colspan: 1 * 2,
    },
  ];
  const columns = [
    {
      key: 'versionName',
      dataKey: 'versionName',
      width: 100,
      align: 'left',
      frozen: 'left',
    },
    // 总货值金额
    {
      key: 'totalValue',
      dataKey: 'totalValue',
      title: '总货值金额',
      align: 'right',
      width: 105,
      headerRenderer: () => (
        <div>
          <div>总货值金额</div>
          <div>(万元)</div>
        </div>
      ),
      dataGetter: ({ rowData }) => {
        let totalValue;
        if (rowData.versionName !== '差异比' && rowData.versionName !== '差异数') {
          totalValue = { value: (rowData.totalValue * 1) / 10000, type: 'number' };
        }
        if (rowData.versionName === '差异数') {
          totalValue = { value: (rowData.totalValueDiff * 1) / 10000, type: 'diff' };
        }
        if (rowData.versionName === '差异比') {
          totalValue = {
            value: (rowData.totalValueDiff * 1) / 10000,
            per: rowData.totalValuePer * 100,
            type: 'per',
          };
        }
        return totalValue;
      },
      cellRenderer: rowData => <DifferenceSingle {...rowData} precision={0} />,
    },
    // 权益货值金额
    {
      key: 'quityValue',
      dataKey: 'quityValue',
      title: '权益货值金额',
      align: 'right',
      width: 105,
      headerRenderer: () => (
        <div>
          <div>权益货值金额</div>
          <div>(万元)</div>
        </div>
      ),
      dataGetter: ({ rowData }) => {
        let quityValue;
        if (rowData.versionName !== '差异比' && rowData.versionName !== '差异数') {
          quityValue = { value: (rowData.quityValue * 1) / 10000, type: 'number' };
        }
        if (rowData.versionName === '差异数') {
          quityValue = { value: (rowData.quityValueDiff * 1) / 10000, type: 'diff' };
        }
        if (rowData.versionName === '差异比') {
          quityValue = {
            value: (rowData.quityValueDiff * 1) / 10000,
            per: rowData.quityValuePer * 100,
            type: 'per',
          };
        }
        return quityValue;
      },
      cellRenderer: rowData => <DifferenceSingle {...rowData} precision={0} />,
    },
    // 房源货值金额
    {
      key: 'houseCommodityValueCount',
      dataKey: 'houseCommodityValueCount',
      title: '房源货值金额',
      align: 'right',
      width: 105,
      headerRenderer: () => (
        <div>
          <div>房源货值金额</div>
          <div>(万元)</div>
        </div>
      ),
      dataGetter: ({ rowData }) => {
        let houseCommodityValueCount;
        if (rowData.versionName !== '差异比' && rowData.versionName !== '差异数') {
          houseCommodityValueCount = {
            value: (rowData.houseCommodityValueCount * 1) / 10000,
            type: 'number',
          };
        }
        if (rowData.versionName === '差异数') {
          houseCommodityValueCount = {
            value: (rowData.houseCommodityValueCountDiff * 1) / 10000,
            type: 'diff',
          };
        }
        if (rowData.versionName === '差异比') {
          houseCommodityValueCount = {
            value: (rowData.houseCommodityValueCountDiff * 1) / 10000,
            per: rowData.houseCommodityValueCountPer * 100,
            type: 'per',
          };
        }
        return houseCommodityValueCount;
      },
      cellRenderer: rowData => <DifferenceSingle {...rowData} precision={0} />,
    },
    // 房源均价
    {
      key: 'houseAvg',
      dataKey: 'houseAvg',
      title: '房源均价',
      align: 'right',
      width: 105,
      headerRenderer: () => (
        <div>
          <div>房源均价</div>
          <div>(元/平米)</div>
        </div>
      ),
      dataGetter: ({ rowData }) => {
        let houseAvg;
        if (rowData.versionName !== '差异比' && rowData.versionName !== '差异数') {
          houseAvg = {
            value: rowData.houseAvg * 1,
            type: 'number',
          };
        }
        if (rowData.versionName === '差异数') {
          houseAvg = {
            value: rowData.houseAvgDiff * 1,
            type: 'diff',
          };
        }
        if (rowData.versionName === '差异比') {
          houseAvg = {
            value: rowData.houseAvgDiff * 1,
            per: rowData.houseAvgPer * 100,
            type: 'per',
          };
        }
        return houseAvg;
      },
      cellRenderer: rowData => <DifferenceSingle {...rowData} precision={0} />,
    },
    // 房源面积
    {
      key: 'saleAreaCount',
      dataKey: 'saleAreaCount',
      title: '房源面积',
      align: 'right',
      width: 105,
      headerRenderer: () => (
        <div>
          <div>房源面积</div>
          <div>(平米)</div>
        </div>
      ),
      dataGetter: ({ rowData }) => {
        let saleAreaCount;
        if (rowData.versionName !== '差异比' && rowData.versionName !== '差异数') {
          saleAreaCount = {
            value: rowData.saleAreaCount * 1,
            type: 'number',
          };
        }
        if (rowData.versionName === '差异数') {
          saleAreaCount = {
            value: rowData.saleAreaCountDiff * 1,
            type: 'diff',
          };
        }
        if (rowData.versionName === '差异比') {
          saleAreaCount = {
            value: rowData.saleAreaCountDiff * 1,
            per: rowData.saleAreaCountPer * 100,
            type: 'per',
          };
        }
        return saleAreaCount;
      },
      cellRenderer: rowData => <DifferenceSingle {...rowData} precision={2} />,
    },
    // 房源套数
    {
      key: 'roomNumCount',
      dataKey: 'roomNumCount',
      title: '房源套数',
      align: 'right',
      width: 90,
      headerRenderer: () => (
        <div>
          <div>房源套数</div>
          <div>(套)</div>
        </div>
      ),
      dataGetter: ({ rowData }) => {
        let roomNumCount;
        if (rowData.versionName !== '差异比' && rowData.versionName !== '差异数') {
          roomNumCount = {
            value: rowData.roomNumCount * 1,
            type: 'number',
          };
        }
        if (rowData.versionName === '差异数') {
          roomNumCount = {
            value: rowData.roomNumCountDiff * 1,
            type: 'diff',
          };
        }
        if (rowData.versionName === '差异比') {
          roomNumCount = {
            value: rowData.roomNumCountDiff * 1,
            per: rowData.roomNumCountPer * 100,
            type: 'per',
          };
        }
        return roomNumCount;
      },
      cellRenderer: rowData => <DifferenceSingle {...rowData} precision={0} />,
    },
    // 车位货值金额
    {
      key: 'parkingValueCount',
      dataKey: 'parkingValueCount',
      title: '车位货值金额',
      align: 'right',
      width: 105,
      headerRenderer: () => (
        <div>
          <div>车位货值金额</div>
          <div>(万元)</div>
        </div>
      ),
      dataGetter: ({ rowData }) => {
        let parkingValueCount;
        if (rowData.versionName !== '差异比' && rowData.versionName !== '差异数') {
          parkingValueCount = {
            value: (rowData.parkingValueCount * 1) / 10000,
            type: 'number',
          };
        }
        if (rowData.versionName === '差异数') {
          parkingValueCount = {
            value: (rowData.parkingValueCountDiff * 1) / 10000,
            type: 'diff',
          };
        }
        if (rowData.versionName === '差异比') {
          parkingValueCount = {
            value: (rowData.parkingValueCountDiff * 1) / 10000,
            per: rowData.parkingValueCountPer * 100,
            type: 'per',
          };
        }
        return parkingValueCount;
      },
      cellRenderer: rowData => <DifferenceSingle {...rowData} precision={0} />,
    },
    // 车位均价
    {
      key: 'parkingAvg',
      dataKey: 'parkingAvg',
      title: '车位均价',
      align: 'right',
      width: 105,
      headerRenderer: () => (
        <div>
          <div>车位均价</div>
          <div>(元/个)</div>
        </div>
      ),
      dataGetter: ({ rowData }) => {
        let parkingAvg;
        if (rowData.versionName !== '差异比' && rowData.versionName !== '差异数') {
          parkingAvg = {
            value: rowData.parkingAvg * 1,
            type: 'number',
          };
        }
        if (rowData.versionName === '差异数') {
          parkingAvg = {
            value: rowData.parkingAvgDiff * 1,
            type: 'diff',
          };
        }
        if (rowData.versionName === '差异比') {
          parkingAvg = {
            value: rowData.parkingAvgDiff * 1,
            per: rowData.parkingAvgPer * 100,
            type: 'per',
          };
        }
        return parkingAvg;
      },
      cellRenderer: rowData => <DifferenceSingle {...rowData} precision={0} />,
    },
    // 车位数
    {
      key: 'parkingNumCount',
      dataKey: 'parkingNumCount',
      title: '车位数',
      align: 'right',
      width: 105,
      headerRenderer: () => (
        <div>
          <div>车位数</div>
          <div>(个数)</div>
        </div>
      ),
      dataGetter: ({ rowData }) => {
        let parkingNumCount;
        if (rowData.versionName !== '差异比' && rowData.versionName !== '差异数') {
          parkingNumCount = {
            value: rowData.parkingNumCount * 1,
            type: 'number',
          };
        }
        if (rowData.versionName === '差异数') {
          parkingNumCount = {
            value: rowData.parkingNumCountDiff * 1,
            type: 'diff',
          };
        }
        if (rowData.versionName === '差异比') {
          parkingNumCount = {
            value: rowData.parkingNumCountDiff * 1,
            per: rowData.parkingNumCountPer * 100,
            type: 'per',
          };
        }
        return parkingNumCount;
      },
      cellRenderer: rowData => <DifferenceSingle {...rowData} precision={0} />,
    },
    // 未推售车位-金额
    {
      key: 'forSaleParkingValueCount',
      dataKey: 'forSaleParkingValueCount',
      title: '金额',
      align: 'right',
      width: 105,
      headerRenderer: () => (
        <div>
          <div>金额</div>
          <div>(万元)</div>
        </div>
      ),
      dataGetter: ({ rowData }) => {
        let forSaleParkingValueCount;
        if (rowData.versionName !== '差异比' && rowData.versionName !== '差异数') {
          forSaleParkingValueCount = {
            value: (rowData.forSaleParkingValueCount * 1) / 10000,
            type: 'number',
          };
        }
        if (rowData.versionName === '差异数') {
          forSaleParkingValueCount = {
            value: (rowData.forSaleParkingValueCountDiff * 1) / 10000,
            type: 'diff',
          };
        }
        if (rowData.versionName === '差异比') {
          forSaleParkingValueCount = {
            value: (rowData.forSaleParkingValueCountDiff * 1) / 10000,
            per: rowData.forSaleParkingValueCountPer * 100,
            type: 'per',
          };
        }
        return forSaleParkingValueCount;
      },
      cellRenderer: rowData => <DifferenceSingle {...rowData} precision={0} />,
    },
    // 未推售车位-车位数
    {
      key: 'forSaleParkingNumCount',
      dataKey: 'forSaleParkingNumCount',
      title: '车位车位数',
      align: 'right',
      width: 105,
      headerRenderer: () => (
        <div>
          <div>车位数</div>
          <div>(个数)</div>
        </div>
      ),
      dataGetter: ({ rowData }) => {
        let forSaleParkingNumCount;
        if (rowData.versionName !== '差异比' && rowData.versionName !== '差异数') {
          forSaleParkingNumCount = {
            value: rowData.forSaleParkingNumCount * 1,
            type: 'number',
          };
        }
        if (rowData.versionName === '差异数') {
          forSaleParkingNumCount = {
            value: rowData.forSaleParkingNumCountDiff * 1,
            type: 'diff',
          };
        }
        if (rowData.versionName === '差异比') {
          forSaleParkingNumCount = {
            value: rowData.forSaleParkingNumCountDiff * 1,
            per: rowData.forSaleParkingNumCountPer * 100,
            type: 'per',
          };
        }
        return forSaleParkingNumCount;
      },
      cellRenderer: rowData => <DifferenceSingle {...rowData} precision={0} />,
    },
  ];
  const headerRenderer = utils.getHeaderRenderer(titleTree, 1);
  const handleTabsKeyChange = key => {
    const cloneAllgoodsValue = _.cloneDeep(allgoodsValue);
    const arr = [];
    const filterAllgoodsValue = _.filter(cloneAllgoodsValue, data => key === data.type);
    if (filterAllgoodsValue && !_.isEmpty(filterAllgoodsValue)) {
      arr.push(
        {
          id: 0,
          versionName: '调整后',
          ...filterAllgoodsValue[0].afterContentVO,
        },
        {
          id: 1,
          versionName: filterAllgoodsValue[0].versionName,
          ...filterAllgoodsValue[0].contentVO,
        },
        {
          id: 2,
          versionName: '差异数',
          ...filterAllgoodsValue[0].differenceVO,
        },
        {
          id: 3,
          versionName: '差异比',
          ...filterAllgoodsValue[0].differenceVO,
        },
      );
    }

    return arr;
  };

  return (
    <BaseTable
      expandColumnKey="name"
      fixHeight={251}
      columns={columns}
      headerHeight={[30, 60]}
      headerRenderer={headerRenderer}
      treeData={handleTabsKeyChange(tabkey)}
    />
  );
};

export default DifferenceOlive;
